#include "GatewayTask.h"
#include "GatewayTaskManager.h"

GatewayTask::GatewayTask(int localfd, const struct sockaddr_in * client, bool buffer) : TCPTaskBuffer(localfd, client, buffer)
{
}

int GatewayTask::verifyConn()
{	// recvtobuf_nopoll; recvtocmd_nopoll
	// error -1 ;    0 timeout
	unsigned char buf[1024];
	if ( socket.recvPacket(buf) <= 0 )
		return -1;

	ReturnLoginCmd ret;
	NullBaseCmd * p = (NullBaseCmd *)buf;
	if ( p->byCmd != LOGIN_CMD || p->byParam != SENDINFO_LOGIN_PARAM_CS )
	{
		sendPacket(&ret, sizeof(ret));
		return -1;
	}
	else
	{
		ret.state = 1; // success login
		if ( sendPacket(&ret, sizeof(ret)) )
			return 1;
		else
			return -1;
	}

	return 1;
}

bool GatewayTask::addToContainer()
{//如果add失败，那就设置disconnect
	return GatewayTaskManager::getInstance()->addTask(this);
}

bool GatewayTask::removeFromContainer()
{
	return GatewayTaskManager::getInstance()->removeTask(this);
}
void GatewayTask::msgParse(const void * cmd, unsigned int len)
{
	GatewayTaskManager::getInstance()->broadcast(cmd, len);
#if 0
	NullBaseCmd * p = (NullBaseCmd *)cmd;
	switch( p->byCmd )
	{
		case LOGIN_CMD:
		{
			ReturnLoginCmd ret;
			switch( p->byParam )
			{
				case SENDINFO_LOGIN_PARAM_CS:
				{
					ret.state = 1;
					if ( sendPacket(&ret, sizeof(ret)))
						verified = true;

					break;
				}
				default:
					sendPacket(&ret, sizeof(ret));
			}
			break;
		}
		default:
			if ( this->verified )
				globalManager.broadcast(cmd,len);
	}
#endif
}

